package org.fengkuok.richworkdesigner.component
{
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	
	import mx.core.UIComponent;
	
	import org.fengkuok.richworkdesigner.model.node.INode;
	
	/**
	 * 流程Node Component基类
	 * @company ZSword (C) Copyright
	 * @author JemiZhuu(周士淳)
	 * @category BaseNodeComponent
	 * @version 1.0
	 * @since 2011-09-04
	 *
	 */
	public class BaseNodeComponent extends UIComponent implements IBaseNodeCompoent
	{		
		private static const selectFilters:Array = [new GlowFilter(0x0781C6)];
		
		public function BaseNodeComponent() {
			super();
			this.buttonMode = true;
			preInit();
		}
				
		protected function preInit():void {
			initDatas();
		}
		
		protected function initUI():void {
		}
		
		protected function initDatas():void {			
		}
		
		override protected function initializationComplete():void {
			super.initializationComplete();
			initUI();			
			initListeners();
		}
		
		protected function initListeners():void {
			this.addEventListener(MouseEvent.MOUSE_DOWN,handleMouseDown);
			this.addEventListener(MouseEvent.CLICK, handleMouseClick);
		}
				
		protected function handleMouseClick(event:MouseEvent):void {
			notifySelected();
			stage.addEventListener(MouseEvent.CLICK, handleStageMouseClick);
		}
		
		protected function handleStageMouseClick(event:MouseEvent):void {
			var obj:Object = event.target;
			if(obj==this) {
				return;
			}
			notifyUnselect();
			stage.removeEventListener(MouseEvent.CLICK, handleStageMouseClick);
		}
		
		/**
		 * 被选中 
		 * 
		 */
		protected function notifySelected():void {
			this.filters = selectFilters;
		}
		
		/**
		 * 取消选中
		 * 
		 */
		protected function notifyUnselect():void {
			this.filters = null;
		}
		
		protected function handleMouseDown(event:MouseEvent):void {			
			stage.addEventListener(MouseEvent.MOUSE_UP, handleMouseUp);
			stage.addEventListener(MouseEvent.MOUSE_MOVE, handleMouseMove);
			this.startDrag();
		}
		
		protected function handleMouseMove(event:MouseEvent):void {
		}				
		
		protected function handleMouseUp(event:MouseEvent):void {			
			this.stopDrag();
			stage.removeEventListener(MouseEvent.MOUSE_UP, handleMouseUp);
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, handleMouseMove);
		}
		
		protected var _node:INode;
		
		public function get node():INode {
			return this._node;
		}
		
		public function set node(node:INode):void {
			this._node = node;
		}
	}
}